package 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import com.bit101.components.PushButton;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import com.bit101.components.List
	import com.bit101.components.ListItem
	import flash.events.MouseEvent;
	import com.bit101.components.Label;
	
	/**
	 * ...
	 * @author yaempechenki@gmail.com
	 */
	public class Main extends Sprite 
	{
		private var textPicture:Sprite;
		
		private var xml:XMLLoader;
		private var loaderImage:Loader2D;
		private var charList:List;
		private var label:Label;
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			stage.addEventListener(Event.RESIZE, onResize);
			
			xml = new XMLLoader();
			xml.addEventListener(Event.COMPLETE, onXmlLoad);
			xml.load("font.fnt");
			
			loaderImage = new Loader2D();
			loaderImage.addEventListener(Event.COMPLETE, onImageLoad);
			loaderImage.goLoad("font.png");
			
			label = new Label(this, 0, 0, "click on left panel..");
		}
		
		private function onXmlLoad(event:Event):void {
			trace("xml load ok");
			var items:Array = [];
			for each (var item:XML in xml.data.chars.char ) 
			{
				items.push(item["@id"]);
			}
			charList = new List(this, 5, 5, items);
			charList.addEventListener(MouseEvent.CLICK, onMouseItemClick);
		}
		
		private function onImageLoad(event:Event):void {
			trace("img load ok");
			textPicture = new Sprite();
			textPicture.addChild(loaderImage.image);
			addChild(textPicture);
			onResize(null);
		}
		
		private function onResize(event:Event):void
		{
			textPicture.x = stage.stageWidth - textPicture.width - 5;
			textPicture.y = 5;
			addChild(textPicture);
			
			charList.y = 5;
			charList.height  =  stage.stageHeight - 10;
			label.x = stage.stageWidth / 2 - label.width / 2;
			label.y = stage.stageHeight - label.height - 5;
		}
		
		private function onMouseItemClick(event:MouseEvent):void {
			if (!(event.target is ListItem)) return;
			
			var listItem:ListItem = event.target as ListItem;
			var charId:int = int(listItem.data);
			
			for each (var item:XML in xml.data.chars.char ) 
			{
				if (charId == item["@id"]) {
					draw(item["@x"], item["@y"], item["@width"], item["@height"], item["@xoffset"], item["@yoffset"], item["@xadvance"]);
					break;
				}
			}
		}
		
		private function draw(xx:int, yy:int, wwidth:int, hheight:int, xOffset:int, yOffset:int, xadvance:int):void {
			var someText:String = "draw:" + " pos[" + xx + "x" + yy + "],   size[" + wwidth + "x" + hheight + "],   offset[" + xOffset + "x" + yOffset + "],   xAdvance " + xadvance;
		
			label.text = someText;
			label.x = stage.stageWidth / 2 - label.width  / 2;
			label.y = stage.stageHeight - label.height - 5;
			
			textPicture.graphics.clear();
			textPicture.graphics.beginFill(0x33FF33, 0.8);
			textPicture.graphics.drawRect(0, 0, textPicture.width, textPicture.height);
			textPicture.graphics.beginFill(0x0000FF, 0.6);
			textPicture.graphics.drawRect(xx , yy, wwidth, hheight);
			textPicture.graphics.endFill();
		}
		
	}
	
}